<template>
  <!-- 商品列表页面 -->
  <div class="com-page">
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="params.query"
            @change="getData()"
            clearable
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchBtn"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addgood">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table
        border
        stripe
        highlight-current-row
        ref="singleTable"
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column type="index" label="#" width="50"> </el-table-column>
        <el-table-column property="goods_name" label="商品名称">
        </el-table-column>
        <el-table-column
          property="goods_price"
          label="商品价格(元)"
          width="220"
        >
        </el-table-column>
        <el-table-column property="goods_weight" label="商品重量" width="220">
        </el-table-column>
        <el-table-column property="add_time" label="创建时间" width="220">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button
              type="primary"
              icon="el-icon-delete"
              @click="deleteGood(scope.row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页按钮开始 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pagenum"
        :page-sizes="[15, 20, 30, 40]"
        :page-size="params.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { request } from "@/utils";
export default {
  components: {},
  props: {},
  data() {
    return {
      tableData: [],
      params: {
        query: "",
        pagenum: 1,
        pagesize: 15,
      },
      total: 0,
    };
  },
  watch: {},
  computed: {},
  methods: {
    async getData() {
      const { data: ret } = await request.get("goods", {
        params: this.params,
      });
      this.tableData = ret.data.goods;
      this.total = ret.data.total;
      console.log(this.tableData);
    },
    // 页码改变触发的方法
    handleSizeChange(e) {
      this.params.pagesize = e;
      this.getData();
    },
    handleCurrentChange(e) {
      this.params.pagenum = e;
      this.getData();
    },
    searchBtn() {
      this.getData();
    },
    deleteGood(id) {
      console.log(id);
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          request.delete(`goods/${id}`).then(() => {
            this.getData();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 跳转增加商品页面
    addgood() {
      this.$router.push("/addgood");
    },
  },
  created() {
    this.getData();
  },
  mounted() {},
};
</script>
<style lang="less" scoped>
.el-pagination {
  margin-top: 20px;
}
</style>